#!/bin/bash
#2020年1月7日20:42:50
#auto change service VIP
#by author www.jfedu.net
########################
ETH_NAME="ens33:1"
NGX_VIP="192.168.1.188"
NGX_MASK="255.255.255.0"
ETH_DIR="/etc/sysconfig/network-scripts"
NGX_NUM=`ps -ef|grep nginx|grep -v grep|grep -v check|wc -l`

start(){
while sleep 4 
do
if [ $NGX_NUM -eq 0 ];then
	ifdown $ETH_NAME
	exit 0	
else
	ping -c 2 $NGX_VIP >/dev/null 2>&1
	if [ $? -ne 0 ];then
cat>$ETH_DIR/ifcfg-$ETH_NAME<<EOF
TYPE="Ethernet"
BOOTPROTO="static"
DEVICE="$ETH_NAME"
IPADDR=$NGX_VIP
NETMASK=$NGX_MASK
ONBOOT="yes"
EOF
	ifup $ETH_NAME
	fi
fi
date
done
}

stop(){
	ifdown $ETH_NAME
	rm -rf $ETH_DIR/ifcfg-$ETH_NAME
}

case $1 in
	start)
	start
	;;
	stop)
	stop
	;;
	*)
	echo -e "\033[32m------------------\033[0m"
	echo -e "\033[32mUsage: /bin/sh $0 {start|stop|help}\033[0m"
    	exit 1 
esac
